package com.action.service.push;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

import com.action.entity.JPushWrap;
import com.action.entity.PushRespond;
import com.action.entity.Redpacket;
import com.action.entity.event.RedPacketChangeEvent;
import com.action.ui.activity.XYBMainActiviy;
import com.base.app_core.model.RedPacketVo;
import com.base.app_core.service.user.model.HomeRxBean;
import com.base.app_core.util.log.F;
import com.xinglai.yun.R;
import com.google.gson.Gson;
import com.hwangjr.rxbus.RxBus;
import com.tencent.map.lib.util.StringUtil;

import cn.jpush.android.api.JPushInterface;

public class JPushMessageReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            String broadAction = intent.getAction();
            if (broadAction.equals(JPushInterface.ACTION_NOTIFICATION_RECEIVED)) {//接收到广播通知。
                Bundle bundle = intent.getExtras();
                String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
                String content = bundle.getString(JPushInterface.EXTRA_ALERT);
                String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
                int notificationId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);

                if (StringUtil.isEmpty(extras)) {
                    F.j("extra为空");
                    return;
                }
                JPushWrap w = new Gson().fromJson(extras, JPushWrap.class);
                if (w == null) {
                    return;
                }
                PushRespond pushRespond = new Gson().fromJson(w.getContent(), PushRespond.class);
                //新红包
                Integer type = pushRespond.getType();
                if (type == null) {
                    return;
                }

                if (type == 0) {
                    System.out.println(pushRespond.toString());
                    RedPacketChangeEvent event = new RedPacketChangeEvent();
                    event.setStatus(RedPacketChangeEvent.PACKET_SEND_ED);
                    Redpacket redpacket = new Redpacket();
                    try {
                        RedPacketVo vo = pushRespond.getObject();
                        if (vo == null) {
                            return;
                        }
                        System.out.println(vo);
                        redpacket.setId(vo.getId());
                        redpacket.setAccount(vo.getAccount());
                        redpacket.setCommentCount(vo.getCommentCount());
                        redpacket.setRange(vo.getRange());
                        redpacket.setWxIcon(vo.getWxIcon());
                        redpacket.setWxNickname(vo.getWxNickname());
                        redpacket.setSn(vo.getSn());
                        redpacket.setCreatedAt(vo.getCreatedAt());
                        redpacket.setContent(vo.getContent());
                        redpacket.setLat(vo.getLat());
                        redpacket.setLng(vo.getLng());
                        redpacket.setGlobalRandomed(false);
                        redpacket.setSenderId(vo.getSenderId());
                        redpacket.setImgs(vo.getImgs());
                        redpacket.setNum(vo.getNum());
                        event.setRedpacket(redpacket);
                        RxBus.get().post(new HomeRxBean(HomeRxBean.SEND_REDPACKET, event));
                        showNotification(context, content, "新红包通知", title);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else if (type == 2) {//分享红包
                    showNotification(context, content, "分享通知", title);
                }
                F.j("title:" + title + " content:" + content + " extras:" + extras + " notificationId:" + notificationId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示一个普通的通知
     *
     * @param context     上下文
     * @param ticker
     * @param title
     * @param contentText
     */
    public static void showNotification(Context context, String ticker, String title, String contentText) {
        Notification notification = new NotificationCompat.Builder(context)
                /**设置通知左边的大图标**/
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher_logo))
                /**设置通知右边的小图标**/
                .setSmallIcon(R.mipmap.ic_launcher_logo)
                /**通知首次出现在通知栏，带上升动画效果的**/
                .setTicker(ticker)
                /**设置通知的标题**/
                .setContentTitle(title)
                /**设置通知的内容**/
                .setContentText(contentText)
                /**通知产生的时间，会在通知信息里显示**/
                .setWhen(System.currentTimeMillis())
                /**设置该通知优先级**/
                .setPriority(Notification.PRIORITY_DEFAULT)
                /**设置这个标志当用户单击面板就可以让通知将自动取消**/
                .setAutoCancel(true)
                /**设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)**/
                .setOngoing(false)
                /**向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置，使用 defaults 属性，可以组合：**/
                .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
                .setContentIntent(PendingIntent.getActivity(context, 1, new Intent(context, XYBMainActiviy.class), PendingIntent.FLAG_CANCEL_CURRENT))
                .build();
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        /**发起通知**/
        notificationManager.notify(0, notification);
    }
}
